/**
 * @overview 创建标注
 * @author dengqiang
 * @description Cesium原生
 * @update  2022.11.15
 */
class GWarStickLabel {
  constructor() {
    window.GWarStickLabelArray = []
  }

  //新增单个标注
  add(paramObj) {
    const { name, parentName, option } = paramObj

    const position = window.cesiumMapTool.car3ToWGS84Two(option.position)

    const rectang = Cesium.Rectangle.fromDegrees(
      position.lng -
        window.cesiumMapTool.trsfMeterToXY({ length: option.label.text.length }).longitude *
          0.5 *
          option.scale,
      position.lat -
        window.cesiumMapTool.trsfMeterToXY({ length: 1 }).latitude * 0.5 * option.scale,
      position.lng +
        window.cesiumMapTool.trsfMeterToXY({ length: option.label.text.length }).longitude *
          0.5 *
          option.scale,
      position.lat + window.cesiumMapTool.trsfMeterToXY({ length: 1 }).latitude * 0.5 * option.scale
    )

    const stickLabelEntity = {
      rectangle: {
        height: position.alt,
        coordinates: rectang,
        material: new Cesium.ImageMaterialProperty({
          image: this.drawText(option.label),
          transparent: true,
          color: { ...Cesium.Color.WHITE },
        }),
        rotation: Cesium.Math.toRadians(option.rotation || 0),
        stRotation: Cesium.Math.toRadians(option.rotation || 0),
      },
    }

    const stickLabel = window.map3DControl.viewer.entities.add(stickLabelEntity)
    stickLabel.attribute = option.attribute || {} //支持添加自定义属性
    stickLabel.attribute.name = name
    stickLabel.attribute.parentName = parentName
    stickLabel.attribute.position = position
    stickLabel.attribute.rotation = option.rotation
    stickLabel.attribute.scale = option.scale
    stickLabel.attribute.labelTextLength = option.label.text.length

    const labelObject = {
      name: name,
      parentName: parentName,
      stickLabel: stickLabel,
    }
    window.GWarStickLabelArray.push(labelObject)
    return stickLabel
  }

  //绘制文字
  drawText(style) {
    const text = style.text
    const c = document.createElement('canvas')
    const d = (text + '').length * style.fontSize
    c.width = d
    c.height = style.fontSize
    const ctx = c.getContext('2d')
    ctx.fillStyle = style.color
    ctx.font = 'bold ' + style.fontSize + 'px 微软雅黑' //设置字体
    ctx.textBaseline = 'hanging' //在绘制文本时使用的当前文本基线
    //绘制文本
    ctx.fillText(text, 0, 0)
    return c
  }

  //根据name移除单个标注
  removeByName(name) {
    window.GWarStickLabelArray.forEach((item, index) => {
      if (item.name == name) {
        window.GWarStickLabelArray.splice(index, 1)
        window.map3DControl.viewer.entities.remove(item.stickLabel)
      }
    })
  }

  //根据parentName移除一批标注
  removeByPatName(parentName) {
    const length = window.GWarStickLabelArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GWarStickLabelArray[i].parentName == parentName) {
        window.map3DControl.viewer.entities.remove(window.GWarStickLabelArray[i].stickLabel)
        window.GWarStickLabelArray.splice(i, 1)
      }
    }
  }

  //移除所有标注
  removeAll() {
    window.GWarStickLabelArray.forEach((item) => {
      window.map3DControl.viewer.entities.remove(item.stickLabel)
    })
    window.GWarStickLabelArray = []
  }

  //根据name控制单个标注的显隐
  setVisibleByName(name, boolean) {
    window.GWarStickLabelArray.forEach((item) => {
      if (item.name == name) {
        item.stickLabel.show = boolean
      }
    })
  }

  //根据parentName控制一批标注的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GWarStickLabelArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.stickLabel.show = boolean
      }
    })
  }

  //根据name设置文字的位置信息
  setPosition(name, newPosition) {
    window.GWarStickLabelArray.forEach((item) => {
      if (item.name === name) {
        const position = window.cesiumMapTool.car3ToWGS84Two(newPosition)

        const labelTextLength = item.stickLabel.attribute.labelTextLength
        const scale = item.stickLabel.attribute.scale

        const rectang = Cesium.Rectangle.fromDegrees(
          position.lng -
            window.cesiumMapTool.trsfMeterToXY({ length: labelTextLength }).longitude * 0.5 * scale,
          position.lat - window.cesiumMapTool.trsfMeterToXY({ length: 1 }).latitude * 0.5 * scale,
          position.lng +
            window.cesiumMapTool.trsfMeterToXY({ length: labelTextLength }).longitude * 0.5 * scale,
          position.lat + window.cesiumMapTool.trsfMeterToXY({ length: 1 }).latitude * 0.5 * scale
        )

        item.stickLabel.rectangle.coordinates = rectang
        item.stickLabel.rectangle.height = position.alt
      }
    })
  }

  //根据name设置文字的旋转角度
  setRotation(name, rotation) {
    window.GWarStickLabelArray.forEach((item) => {
      if (item.name === name) {
        item.stickLabel.rectangle.rotation = Cesium.Math.toRadians(rotation)
        item.stickLabel.rectangle.stRotation = Cesium.Math.toRadians(rotation)
      }
    })
  }

  //根据name设置选中效果
  setActive(name, boolean, time = 100) {
    window.GWarStickLabelArray.forEach((item) => {
      if (item.name == name) {
        window.cesiumMapTool.requestAnimationFrame(
          item.stickLabel.rectangle.material.color,
          boolean,
          time,
          item.stickLabel
        )
      }
    })
  }
}

export default GWarStickLabel
